package cn.ffcs.uec.staff.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.staff.component.bean.UecPunishListboxBean;
import cn.ffcs.uec.staff.constant.UecStaffConstants;
import cn.ffcs.uec.staff.manager.UecPunishManager;
import cn.ffcs.uec.staff.model.UecPunish;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;
@Controller
@Scope("prototype")
public class UecPunishListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = -5811398149406451671L;
	/**
	 * 人员基本信息功能 查询出的实体对象
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 对应页面上的bean
	 */
	private UecPunishListboxBean bean = new UecPunishListboxBean();
	/**
	 * 人员处罚情况实体
	 */
	private UecPunish uecPunish;
	/**
	 * 人员处罚情况查询实体
	 */
	 private UecPunish qryUecPunish;

	private UecPunishManager uecPunishManager = (UecPunishManager) ApplicationContextUtil
			.getBean("uecPunishManager");

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecPunishListboxExt() {
		Executions.createComponents(UecStaffConstants.UEC_PUNISH_EXT_LISTBOX_ZUL,
				this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.addForward(UecStaffConstants.ON_UEC_PUNISH_QUERY, this,
				UecStaffConstants.ON_UEC_PUNISH_QUERY_RESPONSE);

		this.addForward(UecStaffConstants.ON_CLEAN_UEC_PUNISH, this,
				UecStaffConstants.ON_CLEAN_UEC_PUNISH_RESPONSE);

	}

	/**
	 * 初始化
	 */
	public void onCreate() {
		this.setButtonValid(false, false, false, false);
	}

	/**
	 * 选择查询记录
	 * 
	 * @throws Exception
	 */
	public void onSelectUecPunishList() throws Exception {
		if (this.bean.getUecPunishListbox().getSelectedCount() > 0) {
			uecPunish = (UecPunish) bean.getUecPunishListbox().getSelectedItem()
					.getValue();
		}
	}

	/**
	 * ，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecPunishQueryResponse(ForwardEvent event) {
		uecStaffInfo = (UecStaffInfo) event.getOrigin().getData();
		if (null != uecStaffInfo) {
			if (!StrUtil.isEmpty(uecStaffInfo.getPkPsndoc())) {//判断是否为集团数据
				this.setButtonValid(true, false, false,false);
			}else {
				this.setButtonValid(true, true,true,true);
			}
			qryUecPunish = new UecPunish();
			qryUecPunish.setStaffId(uecStaffInfo.getStaffId());
			if (!StrUtil.isEmpty(uecStaffInfo.getPkPsndoc())) {
				qryUecPunish.setPkPsndoc(uecStaffInfo.getPkPsndoc());
			}
			this.onQueryUecPunish();
		}
	}
	/**
	 * 清除
	 * 
	 * @param event
	 */
	public void onCleanUecPunishResponse(ForwardEvent event) {
		clearVariable();
		uecStaffInfo = null;
		ListboxUtils.clearListbox(this.bean.getUecPunishListbox());
		this.bean.getUecPunishListboxPaging().setTotalSize(1);
	}

	/**
	 * 设置按钮
	 * 
	 * @param canAdd
	 * @param candel
	 */
	private void setButtonValid(boolean canView, boolean canAdd,
			boolean canEdit, boolean candel) {
		this.bean.getViewButton().setDisabled(!canView);
		this.bean.getAddButton().setDisabled(!canAdd);
		this.bean.getEditButton().setDisabled(!canEdit);
		this.bean.getDelButton().setDisabled(!candel);
	}

	/**
	 * 查询按钮
	 */
	public void onQueryUecPunish() {
		this.bean.getUecPunishListboxPaging().setActivePage(0);
		this.onQueryUecPunishList();
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecPunish() {
		ListboxUtils.clearListbox(this.bean.getUecPunishListbox());
		this.bean.getUecPunishListboxPaging().setTotalSize(1);
	}
	/**
	 * 查看按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onViewUecPunish() throws Exception, InterruptedException {
		openUecEditWin("view");
	}

	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onAddUecPunish() throws Exception {
		openUecEditWin("add");
	}
	/**
	 * 编辑按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onEditUecPunish() throws Exception, InterruptedException {
		openUecEditWin("edit");
	}
	/**
	 * 
	 * @param opType
	 */
	private void openUecEditWin(String opType) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("opType", opType);
		if ("view".equals(opType) || "edit".equals(opType)) {
			if (null != uecPunish) {
				map.put("uecPunish", uecPunish);
			} else {
				ZkUtil.showError("请选择一条记录！", "提示信息");
				return;
			}
		} else if ("add".equals(opType)) {
			map.put("uecStaffInfo", uecStaffInfo);
		}
		Window window = (Window) Executions.createComponents(
				UecStaffConstants.UEC_PUNISH_EDIT_ZUL, this, map);
		try {
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						// 新增或修改完成后，列表从数据库更新
						uecPunish = (UecPunish) event.getData();
						onQueryUecPunishList();
					}
				}
			});
		} catch (SuspendNotAllowedException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 删除按钮
	 */
	public void onDelUecPunish() {
		if (this.uecPunish != null && uecStaffInfo != null) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						uecPunish.removeOnly();
						PubUtil.reDisplayListbox(bean.getUecPunishListbox(),
								uecPunish, "del");
						uecPunish = null;
					}
				}
			});
		} else {
			ZkUtil.showError("请选择你要删除的记录", "提示信息");
			return;
		}
	}

	/**
	 * 翻页
	 */
	public void onUecPunishListboxPaging() {
		this.onQueryUecPunishList();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecPunish = null;
	}

	/**
	 * 查询
	 */
	public void onQueryUecPunishList() {
		if (null != qryUecPunish) {
			PageInfo pageInfo = uecPunishManager.queryPageInfoByUecPunish(qryUecPunish,
					this.bean.getUecPunishListboxPaging().getActivePage() + 1,
					this.bean.getUecPunishListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(),
					true);
			this.bean.getUecPunishListbox().setModel(list);
			this.bean.getUecPunishListboxPaging().setTotalSize(
					pageInfo.getTotalCount());
		}
	}

}
